var OrderTable = {
    id: "oderTable", //表格id
    seItem: null,		//选中的条目
    me : "",
    href:"",
    countIndex:""
};

/**
 * 初始化整个表格
 */
OrderTable.init = function(current){
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(OrderTable.id,Feng.ctxPath+"/order/allParam",defaultColunms);
    bstable.setTopView(false,false,false);
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
OrderTable.initColumn = function(){
    var columns = [
        {title: '序号',align: 'center',valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index+1;
            }},
        {title: '订单编号',field: 'orderNum',align: 'center',valign: 'middle'},
        {title: '购买学员',field: 'userName',align: 'center',valign: 'middle'},
        {title: '下单时间',field: 'orderDate',align: 'center',valign: 'middle'},
        {title: '支付时间',field: 'payDate',align: 'center',valign: 'middle'},
        {title: '订单金额',field: 'actualOrderAmount',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                return numeral(row.actualOrderAmount - row.refundsAmount).format('0,0.00');
            }},
        {title: '支付方式',field: 'payment',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                if(value == "1"){
                    return "现金支付";
                }else if(value == "2"){
                    return "微信支付";
                }else if(value == "3"){
                    return "公司转账"
                }
            }},
        {title: '订单状态',field: 'orderState',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                if(value == "1"){
                    return "<span class='label label-danger'>未支付</span>";
                }else if(value == "2"){
                    return "<span class='label label-success'>已支付</span>";
                }else if(value == "3"){
                    return "<span class='label label-info'>已退款</span>";
                }else if(value == "4"){
                    return "<span class='label label-default'>已取消</span>";
                }else if(value == "5"){
                    return "<span class='label label-warning'>退款中</span>";
                }
            }},
        {title: '操作',field: '#',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                var e = '<button class="btn btn-primary btn-xs" onclick="editBtn(\''+ row.id + '\')" title="详情"><i class="glyphicon glyphicon-pencil"></i></button> &nbsp;&nbsp;';
                return e;
            }
        }];
    return columns;
};

/**
 * 点击编辑按钮时，跳出用户详情对话框
 * @returns
 */
function editBtn(uid){
    var id = uid;
    var index = layer.open({
        type: 1,
        title: '订单详情',
        skin: 'layui-layer-lan',
        area: [OrderInfoDlg.width,OrderInfoDlg.height],
        content: OrderInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            OrderInfoDlg.renderAdd();
            OrderInfoDlg.load(uid);

        },
        maxmin:true,
        scrollbar: false,
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            OrderInfoDlg.editSubmit();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    layer.full(index);
    OrderInfoDlg.currentLayerIndex = index;
}

/**
 * 点击搜索按钮
 */
function clickSearch(){
    var queryData = {};
    //订单号
    var val_search_orderNum = $("#txt_search_orderNum").val();
    if (val_search_orderNum !== "" && val_search_orderNum !== null) {
        queryData['orderNum'] =  val_search_orderNum;
    }
    //学员名称
    var val_search_studentName = $("#txt_search_studentName").val();
    if (val_search_studentName !== "" && val_search_studentName !== null) {
        queryData['userName'] = val_search_studentName;
    }
    //订单状态
    var val_search_orderState = $("#txt_search_orderState").find("option:selected").val();
    if (val_search_orderState !== "" && val_search_orderState !== null && val_search_orderState !== "-1") {
        queryData['orderState'] = val_search_orderState;
    }
    //学员手机号
    var val_search_studentPhone = $("#txt_search_studentPhone").val();
    if (val_search_studentPhone !== "" && val_search_studentPhone !== null) {
        queryData['phone'] = val_search_studentPhone;
    }
    //支付方式
    var val_search_payType = $("#txt_search_payType").find("option:selected").val();
    if (val_search_payType !== "" && val_search_payType !== null && val_search_payType !== "-1") {
        queryData['payment'] = val_search_payType;
    }
    //下单开始时间
    var val_search_startDate = $("#txt_search_startDate").val();
    if (val_search_startDate !== "" && val_search_startDate !== null) {
        queryData['startDate'] = val_search_startDate;
    }
    //下单结束时间
    var val_search_endDate = $("#txt_search_endDate").val();
    if (val_search_endDate !== "" && val_search_endDate !== null) {
        queryData['endDate'] = val_search_endDate;
    }
    var countIndex = OrderTable.countIndex;
    if (countIndex === 1) {//今日订单
        var d = new Date();
        var str = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
        queryData['orderDate'] = str;
    } else if (countIndex === 2) {//未付款
        queryData['orderState'] = 1;
    } else if (countIndex === 3) {//已付款
        queryData['orderState'] = 2;
    }
    console.log(queryData);
    OrderTable.me.destroy(queryData);
    // OrderTable.me.refresh({query: queryData});
}

/**
 * 清空搜索栏
 */
function resetSearchVal() {
    //订单号
    $("#txt_search_orderNum").val("");
    //退款人
    $("#txt_search_studentName").val("");
    //退款状态
    $("#txt_search_orderState").val("-1");
    //退款时间
    $("#txt_search_studentPhone").val("");
    //支付方式
    $("#txt_search_payType").val("-1");
    //下单开始时间
    $("#txt_search_startDate").val("");
    //下单结束时间
    $("#txt_search_endDate").val("");
}



$(function(){
    OrderTable.init(1);
    OrderInfoDlg.init();
    ChangeCourseDlg.init();
    ReduceAmountDlg.init();
    //缓存课程管理页面地址
    OrderTable.href = $("#iframe_tab_10211").attr("src");

    $(".nav-tabs li").on("click", function () {
        OrderTable.countIndex = $(this).index() || "";
        clickSearch();
    });
});
